﻿Imports System.Xml.Serialization
Imports System.IO

Public Module LoadSave

	Private _filename As String
	Public Property Filename As String
		Set(value As String)
			_filename = value
			AppSettings.PathToLastKeyboardSettings = value
		End Set
		Get
			Return _filename
		End Get
	End Property

	Public AppSettings As New ApplicationSettings

	<Serializable>
 Public Class Definice
		<XmlElement(ElementName:="Keyboards")>
		Public Property Keyboards As New List(Of Keyboard)

		<XmlElement(ElementName:="RightModifierKeysEqualsLeft")>
		Public Property RightModifierKeysEqualsLeft As Boolean

		<XmlElement(ElementName:="StartKeySequenceWithModifierKeys")>
		Public Property StartKeySequenceWithModifierKeys As Boolean
	End Class

	<Serializable>
	Public Class Keyboard
		<XmlElement(ElementName:="Name")>
		Public Property Name As String

		<XmlElement(ElementName:="HwID")>
		Public Property HwID As String

		<XmlElement(ElementName:="Enabled")>
		Public Property Enabled As Boolean

		<XmlElement(ElementName:="Profiles")>
		Public Property Profiles As New List(Of Profile)
	End Class

	<Serializable>
	Public Class Profile
		<XmlElement(ElementName:="Name")>
		Public Property Name As String

		<XmlElement(ElementName:="Enabled")>
		Public Property Enabled As Boolean

		<XmlElement(ElementName:="Inputs")>
		Public Property Inputs As New List(Of Input)
	End Class

	<Serializable>
	Public Class Input
		<XmlElement(ElementName:="Name")>
		Public Property Name As String

		<XmlElement(ElementName:="Enabled")>
		Public Property Enabled As Boolean

		<XmlElement(ElementName:="Strokes")>
		Public Property Strokes As New List(Of Interception.InterceptionKeyStroke)

		<XmlElement(ElementName:="Outputs")>
		Public Property Outputs As New List(Of Output)
	End Class

	<Serializable>
	Public Class Output
		<XmlElement(ElementName:="Enabled")>
		Public Property Enabled As Boolean

		<XmlElement(ElementName:="Strokes")>
		Public Property Strokes As New List(Of Interception.InterceptionKeyStroke)
	End Class


	<Serializable>
	Public Class ApplicationSettings
		<XmlElement(ElementName:="PathToLastKeyboardSettings")>
		Public Property PathToLastKeyboardSettings As String

		<XmlElement(ElementName:="AutoStart")>
		Public Property AutoStart As Boolean

	End Class

	Public Sub UlozNastaveni()
		Try
			Dim shlp As String = AppDomain.CurrentDomain.BaseDirectory & "\AppSettings.xml"
			UlozTriduDoXML(shlp, AppSettings)
		Catch ex As Exception
			MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
		End Try
	End Sub

	Public Sub NactiNastaveni()
		Try
			Dim shlp As String = AppDomain.CurrentDomain.BaseDirectory & "\AppSettings.xml"
			If File.Exists(shlp) Then
				AppSettings = NactiTriduZXML(AppDomain.CurrentDomain.BaseDirectory & "\AppSettings.xml", New ApplicationSettings)
				Filename = AppSettings.PathToLastKeyboardSettings
			End If
		Catch ex As Exception
			MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
		End Try
	End Sub


	Public Sub UlozTriduDoXML(FilePath As String, Trida As Object)
		Dim ser As New XmlSerializer(Trida.GetType)
		Dim File As New IO.StreamWriter(FilePath)
		ser.Serialize(File, Trida)
		File.Dispose()
	End Sub

	Public Function NactiTriduZXML(FilePath As String, Trida As Object) As Object
		' v pohode lze nacitat starsi verze trid Velicina a Instalace, pokud nejsou jejich vlastnosti(promenne) pouzivany pro neco
		'   jineho nez je nacitani nebo ukladani do XML
		Try
			Dim ser As New XmlSerializer(Trida.GetType)
			Dim File = New IO.StreamReader(FilePath)
			NactiTriduZXML = ser.Deserialize(File)
			File.Dispose()
		Catch
			Return Nothing
		End Try
	End Function


End Module
